



#include <material_editor/materialview.h>
#include <material_editor/materialeditor.h>
#include <genericdeletedialog.h>
#include <genericrenamedialog.h>
#include <genericsavedialog.h>
#include <session.h>



void MaterialView::MaterialEdit (Material *material)
{
  if (!OpenEditor(material))
    {
      InsertEditor(new MaterialEditor (material, this));
    }
}



void MaterialView::MaterialEditCurrent ()
{
  MaterialItem* item = get_current_item<MaterialItem>(_overview);
  if (!item)
    {
      return;
    }
  MaterialEdit (item->GetMaterial());
}



void MaterialView::MaterialAdd()
{
  CategoryItem* item = get_current_item<CategoryItem>(_overview);
  if (!item)
    {
      return;
    }

  GenericRenameDialog dlg (Session::Get()->GetMainWindow());
  dlg.SetType("Create new material", "");
  dlg.SetName("New Material");
  dlg.SetCategory(item->GetCategory());
  if (dlg.exec() == QDialog::Rejected)
    {
      return;
    }


  _selectNewItems = true;
  Material* material = _manager->CreateMaterial();
  material->SetName(dlg.GetName());
  material->SetCategory(dlg.GetCategory());

  if (_manager->AddMaterial(material))
    {
      MaterialEdit(material);
    }
  _selectNewItems = false;
}

void MaterialView::MaterialAdded(Material *material)
{
  AddItem (new MaterialItem(this, material), material);
}

void MaterialView::EditMaterial(Material *material)
{
  MaterialEdit(material);
}
